package com.jk.controller;

import com.jk.bean.Log;
import com.jk.utils.ReceivePage;
import com.jk.utils.SendPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("mg")
public class MongoController {

    @Autowired
    private MongoTemplate mongoTemplate;


    @ResponseBody
    @RequestMapping("queryLog")
    public SendPage queryLog(ReceivePage rp){

        Query query = new Query();
        int count = (int) mongoTemplate.count(query, Log.class);

       query.skip((rp.getPage()-1)*rp.getRows());
       query.limit(rp.getRows());
        List<Log> logs = mongoTemplate.find(query, Log.class);

        return new SendPage(count,logs);
    }

    @ResponseBody
    @RequestMapping("delLog")
    public String delLog(String ids){

        Query query = new Query();
        mongoTemplate.remove(query.addCriteria(Criteria.where("id").in(ids)),Log.class);

        return "1";
    }


}
